/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"20E73BEB-F269-4837-AE33-0DDA78CC7E93"}
 */
var $separador_sql_params = "#$s3p4r4d0r$#"

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"301A009D-7903-4B09-BD17-0BD20487C754"}
 */
var $error = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"2C524E1B-FB4F-4048-9C2D-EC4C492BF9CC"}
 */
var $MODO_LECTURA = "MODO_LECTURA";

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"400A4548-244C-4B09-B23C-CFC325B2B3F1"}
 */
var $MODO_EDICION = "MODO_EDICION";

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"02BD2525-EC4F-46FE-98BD-1EA61FB86548"}
 */
var $MODO_ALTA = "MODO_ALTA";

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"3E4DA713-7C39-413B-BA1D-265F917F164F"}
 */
var $MODO_BAJA = "MODO_BAJA";

/**
 * Formulario principal activo (con ó sin navigator)
 * @type {String}
 *
 * @properties={typeid:35,uuid:"DE188ECD-8D22-454A-B193-FA3986A37602"}
 */
var $frm_siv_main = '';

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"4394B546-3619-4401-AD8D-1516DBD0E9A9"}
 */
var $NOMBRE_SERVIDOR = "pigeon_board";

/**
 * @type {JSRecord<db:/pigeon_board/adm_t900_usuarios>}
 *
 * @properties={typeid:35,uuid:"15D57F1A-6156-4147-BAEA-8BB50756EF5A",variableType:-4}
 */
var $record_adm_t900_usuarios;

/**
 * @type {UUID}
 *
 * @properties={typeid:35,uuid:"D7A60DF5-9FA7-48FF-959D-92DCF5F865C7",variableType:-4}
 */
var $adm_t900_usuarios_id;

/**
 * @param {String} formulario
 *
 * @properties={typeid:24,uuid:"5D03BD92-A0F9-4455-B2E4-02929F24C2BC"}
 */
function doEdit(formulario) {

	if (!globals.isEditing()) globals.startEditing();
	/**@type {Array<String>}*/
	var allNames = forms[formulario].elements.allnames
	for (var i = 0; i < allNames.length; i++) {
		if (allNames[i].indexOf('fld_') >= 0) {
			forms[formulario].elements[allNames[i]].bgcolor = '#ffeddb';
			forms[formulario].elements[allNames[i]]["readOnly"] = false;
			forms[formulario].elements[allNames[i]]["fgcolor"] = '#666666';
		}
		if (allNames[i].indexOf('vlst_') >= 0) {
			forms[formulario].elements[allNames[i]].bgcolor = '#ffeddb';
			forms[formulario].elements[allNames[i]]["readOnly"] = false;
			forms[formulario].elements[allNames[i]]["fgcolor"] = '#666666';
		}
		if (allNames[i].indexOf('chk_') >= 0) {
			forms[formulario].elements[allNames[i]].bgcolor = '#ffeddb';
			forms[formulario].elements[allNames[i]]["readOnly"] = false;
			forms[formulario].elements[allNames[i]]["fgcolor"] = '#666666';
		}
	}
}

/**
 * @param {String} formulario
 *
 * @properties={typeid:24,uuid:"6CDCDBBD-4818-4F63-876E-32E5BB55F5F6"}
 */
function hide_btn_reset_fields(formulario){
	
	/**@type {Array<String>}*/
	var allNames = forms[formulario].elements.allnames;
	for (var i = 0; i < allNames.length; i++) {
		if (allNames[i].indexOf('fld_') >= 0) {
			forms[formulario].elements[allNames[i]]["readOnly"] = true;
			forms[formulario].elements[allNames[i]]["bgcolor"] = '#F5F7F9';
			forms[formulario].elements[allNames[i]]["fgcolor"] = '#666';
		}
		if (allNames[i].indexOf('vlst_') >= 0) {
			forms[formulario].elements[allNames[i]].bgcolor = '#F5F7F9';
			forms[formulario].elements[allNames[i]]["readOnly"] = true;
			forms[formulario].elements[allNames[i]]["fgcolor"] = '#666';
		}
		if (allNames[i].indexOf('chk_') >= 0) {
			forms[formulario].elements[allNames[i]].bgcolor = '#F5F7F9';
			forms[formulario].elements[allNames[i]]["readOnly"] = true;
			forms[formulario].elements[allNames[i]]["fgcolor"] = '#666';
		}
	}
}

/**
 * @author Roberto J. Blasco
 * @version 1.0
 * @param {String} formulario
 * @param {String} element_name
 *
 * @properties={typeid:24,uuid:"D5304FB9-2DFA-4DAC-893F-96D87CC518E3"}
 */
function hide_btn_reset_fields_elment(formulario, element_name){
	
	forms[formulario].elements[element_name]["readOnly"] = true;
	forms[formulario].elements[element_name]["bgcolor"] = '#F5F7F9';
	forms[formulario].elements[element_name]["fgcolor"] = '#666';
}

/**
 * @author Roberto J. Blasco
 * @version 1.0
 * @param {String} formulario
 * @param {String} element_name
 *
 * @properties={typeid:24,uuid:"CC929CDD-C424-4916-A54E-29B6A44F7225"}
 */
function doEdit_element(formulario, element_name) {

	forms[formulario].elements[element_name].bgcolor = '#e4e4c9';
	forms[formulario].elements[element_name]["readOnly"] = false;
	forms[formulario].elements[element_name]["fgcolor"] = '#666666';
}

/**
 * @properties={typeid:24,uuid:"ED8E3A96-A356-4844-9117-553E839D760A"}
 */
function isEditing() {
	return !databaseManager.getAutoSave();
}

/**
 * @properties={typeid:24,uuid:"0F41B45D-11C6-4AEF-976B-720456F84F85"}
 */
function startEditing() {
	databaseManager.setAutoSave(false);
}

/**
 * @param {JSFoundSet} foundset
 * @return {Boolean}
 *
 * @properties={typeid:24,uuid:"EEE6AEB1-F3E6-4EF8-8804-95BCF0CBA6E0"}
 */
function saveEdits_foundset(foundset) {

	var success = databaseManager.saveData(foundset);
	
	if (!success) {
		var array_failed_records = databaseManager.getFailedRecords();
		var txt_error = "";
		for (var x = 0; x < array_failed_records.length; x++) {
			/** @type {JSRecord}*/
			var js_record = array_failed_records[x];
			txt_error += js_record.exception.getMessage() + "\n";
		}
		plugins.dialogs.showErrorDialog("Error al guardar ...", txt_error, "Aceptar");
		return false;
	}

	databaseManager.setAutoSave(true);
	return true;
}

/**
 * @param {JSRecord} record
 * @return {Boolean}
 *
 * @properties={typeid:24,uuid:"6CBD9C57-46FE-4AA5-9038-52D58E5F7650"}
 */
function saveEdits_record(record) {

	var success = databaseManager.saveData(record);
	
	if (!success) {
		var array_failed_records = databaseManager.getFailedRecords();
		var txt_error = "";
		for (var x = 0; x < array_failed_records.length; x++) {
			/** @type {JSRecord}*/
			var js_record = array_failed_records[x];
			txt_error += js_record.exception.getMessage() + "\n";
		}
		plugins.dialogs.showErrorDialog("Error al guardar ...", txt_error, "Aceptar");
		return false;
	}

	databaseManager.setAutoSave(true);
	return true;
}

/**
 * @return {Boolean}
 *
 * @properties={typeid:24,uuid:"459E2D71-3CF3-47DA-B197-4C778E236049"}
 */
function saveEdits() {

	var success = databaseManager.saveData();
	
	if (!success) {
		var array_failed_records = databaseManager.getFailedRecords();
		var txt_error = "";
		for (var x = 0; x < array_failed_records.length; x++) {
			/** @type {JSRecord}*/
			var js_record = array_failed_records[x];
			txt_error += js_record.exception.getMessage() + "\n";
		}
		plugins.dialogs.showErrorDialog("Error al guardar ...", txt_error, "Aceptar");
		return false;
	}

	databaseManager.setAutoSave(true);
	return true;
}

/**
 * @param {JSFoundSet} foundset
 *
 * @properties={typeid:24,uuid:"B3577EE0-B0DB-4132-B419-2D6300FFE322"}
 */
function cancelEditing_foundset(foundset) {
	
	databaseManager.revertEditedRecords(foundset);
	databaseManager.setAutoSave(true);
}

/**
 * @properties={typeid:24,uuid:"C2B33557-C8C5-4503-8F43-7CB943B0C265"}
 */
function cancelEditing() {
	
	databaseManager.revertEditedRecords();
	databaseManager.setAutoSave(true);
}

/**
 * Muestra el formulario en pantalla
 * @author Roberto J. Blasco
 * @version 1.0
 * @param {JSEvent} event
 * @param {String} frm Formulario destino
 * @param {JSFoundSet} foundset	Foundset del Formulario
 * @param {String} [modo] Modo de visualización
 * @param {JSDataSet} [ds_valores_iniciales]	Valores iniciales en caso de que los hubiera cuando el modo es de ALTA
 * @param {Boolean} [volver_tras_edicion]
 * @properties={typeid:24,uuid:"57585488-C64A-4D22-88F3-A148762997E1"}
 * @AllowToRunInFind
 */
function ir_formulario(event, frm, foundset, modo, ds_valores_iniciales, volver_tras_edicion) {
	
	if (modo == null){
		modo = globals.$MODO_LECTURA;
	}
	
	if (volver_tras_edicion == null){
		volver_tras_edicion = false;
	}
	
	forms[globals.$frm_siv_main].elements.tab_principal.removeAllTabs();
	if (modo == globals.$MODO_LECTURA){
		forms[frm].foundset.loadRecords(foundset);
		if (forms[frm]._hide_btn_reset_fields){
			forms[frm]._hide_btn_reset_fields();
		}
	}else if (modo == globals.$MODO_EDICION){
		forms[frm].foundset.loadRecords(foundset);
		if (forms[frm]._doEdit){
			forms[frm]._doEdit();
		}
	}else if (modo == globals.$MODO_ALTA){
		forms[frm].foundset.newRecord();
		// Establecemos valores iniciales en alta en caso de que los hubiera
		if (ds_valores_iniciales != null){
			for (var x = 1; x <= ds_valores_iniciales.getMaxRowIndex(); x++){
				forms[frm].foundset.setDataProviderValue(ds_valores_iniciales.getValue(x,1),ds_valores_iniciales.getValue(x,2))
			}
		}
		if (forms[frm]._doEdit){
			forms[frm]._doEdit();
		}
	}else if (modo == globals.$MODO_BAJA){
		
		if (forms[frm]._hide_btn_reset_fields){
			forms[frm]._hide_btn_reset_fields();
		}
		
		
	}
	
	forms[frm].$formulario_padre.frm = event.getFormName();
	forms[frm].$formulario_padre.foundset = forms[event.getFormName()].foundset;
	forms[frm].$formulario_padre.volver_tras_edicion = volver_tras_edicion;
	forms[globals.$frm_siv_main].elements.tab_principal.addTab(forms[frm]);
}

/**
 * Devuelve el html correspondiente al menú
 * @author Roberto J. Blasco
 * @version 1.0
 * @param {String} texto
 *
 * @properties={typeid:24,uuid:"95C3A6F2-8948-4A1E-ABF5-D3F090E28F3D"}
 */
function set_menu_html(texto){
	
	return <html>
				<span style = "font-size: 8px; color: #fff; font-weight: bold">
				{texto}
				</span>
			</html>;
}
/**
 * @properties={typeid:24,uuid:"C38EEE73-74EA-4C3E-A44A-36139A8F3C94"}
 * @param {JSEvent} event
 * @param {String} frm
 * @param {JSFoundSet} foundset
 * @param {Number} modo
 * @param {String} msg_titulo
 * @param {String} msg_body
 * @return {Boolean}
 */
function mostrar_notificacion(event, frm, foundset, modo, msg_titulo, msg_body) {
	
//	forms.template_notificacion.props.event = event;
//	forms.template_notificacion.props.frm = frm;
//	forms.template_notificacion.props.foundset = foundset;
//	forms.template_notificacion.props.modo = modo;
//	forms.template_notificacion.props.msg_titulo = msg_titulo;
//	forms.template_notificacion.props.msg_body = msg_body;
//	forms.template_notificacion.controller.show();
	
	return forms.template_notificacion.props.bok;
}
